package database;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sharedspace.Test;

import  messages.MessageAllocateManualTest;

public class setFileFromClient {
	
	private String directory;
	
	public setFileFromClient(String directory)
	{
		this.directory = directory;
	}

	 public String setFileFromClient(MessageAllocateManualTest MsgAmt) 
	 {
		 
		 try{
		 Test test = MsgAmt.getManualTest();
		 
		 if (!new File("aes//Tests//").exists()) //if test files were not created
			{
			 new File("aes//Tests//").mkdir();
				if (!new File("aes//Tests//"+test.getTestID()+"//").exists())//if file not already exists
					new File("aes//Tests//"+test.getTestID()+"//").mkdir(); //create the directory 
			}
		 	new File("aes//Tests//"+test.getTestID()+"//").mkdir(); //create the directory
		 	File file =new File("aes//Tests//"+test.getTestID()+"//"+MsgAmt.getManualTest().getTestID()+".doc");
		 	
			BufferedOutputStream bos;
			bos = new BufferedOutputStream(new FileOutputStream(file));
			bos.write(MsgAmt.getManualTest().getTeacherFileByteArray(), 0 , MsgAmt.getManualTest().getTeacherFileByteArray().length); //write out file
		    bos.flush(); //close buffer
		    bos.close();
		 return "aes//Tests//"+test.getTestID()+"//"+MsgAmt.getManualTest().getTestID()+".doc";
		 }catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return null;
	 }
}
